//============================================================================================
/**
 * @file	guinness_call.c
 * @brief	ギネス呼び出し
 * @author	Nozomu Saito
 *
 * モジュール名：GuinnessCall_
 */
//============================================================================================
#include "common.h"
#include "fieldsys.h"
#include "field/field.h"

#include "system/wipe.h"
#include "system/brightness.h"

#include "guinness_call.h"

#include "application/guinness/guinness_app.h"	//for GNS_APP_HEADER

typedef struct GNS_CALL_EVT_WORK_tag
{
	int Seq;
	GNS_APP_HEADER *Header;
}GNS_CALL_EVT_WORK;

static BOOL GuinnessEvent(GMEVENT_CONTROL * event);

//==============================================================================
/**
 * イベント作成
 *
 * @param	fsys		フィールドシステムポインタ
 * @param	inMode
 * @param	inFirstPage
 *
 * @retval  none
 */
//==============================================================================
void GuinnessCall_SetupEvent(FIELDSYS_WORK * fsys, const u8 inMode, const u8 inFirstPage)
{
	// イベントワーク作成
	GNS_CALL_EVT_WORK *wk = sys_AllocMemoryLo(HEAPID_WORLD, sizeof(GNS_CALL_EVT_WORK));
	wk->Seq = 0;
	wk->Header = sys_AllocMemoryLo(HEAPID_WORLD,sizeof(GNS_APP_HEADER));
	wk->Header->SaveData = GameSystem_GetSaveData(fsys);
	wk->Header->Mode = inMode;
	wk->Header->FirstPage = inFirstPage;
	FieldEvent_Call(fsys->event, GuinnessEvent, wk);
}

//==============================================================================
/**
 * イベント
 *
 * @param	@param	event	イベントポインタ
 *
 * @retval  BOOL	TRUE:イベント終了　FALSE：イベント継続
 */
//==============================================================================
static BOOL GuinnessEvent(GMEVENT_CONTROL * event)
{
	FIELDSYS_WORK * fsys = FieldEvent_GetFieldSysWork(event);
	GNS_CALL_EVT_WORK *wk  = FieldEvent_GetSpecialWork(event);

	switch(wk->Seq){
	case 0:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEOUT,
				WIPE_TYPE_FADEOUT,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_FIELD
				);
		wk->Seq++;
		break;
	case 1:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		Guinness_SetProc(fsys, wk->Header);
		FieldSystemProc_SeqHoldEnd();
		wk->Seq++;
		break;
	case 2:
		//プロセス終了待ち
		if(FieldEvent_Cmd_WaitSubProcEnd(fsys)){
			break;
		}
		wk->Seq++;
		//フィールドマッププロセス復帰
		FieldEvent_Cmd_SetMapProc(fsys);
		break;
	case 3:
		//フィールドプロセス開始終了待ち
		if(FieldEvent_Cmd_WaitMapProcStart(fsys)){
			//ブラックインリクエスト
			WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,HEAPID_FIELD
				);
			wk->Seq++;
			break;
		}
		break;
	case 4:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		// イベントワーク解放
		sys_FreeMemoryEz(wk->Header);
		sys_FreeMemoryEz(wk);
		return TRUE;
	}

	return FALSE;
}